//
//  LLComposerTextView.swift
//  xinlangweibo
//
//  Created by 任玉林 on 16/7/3.
//  Copyright © 2016年 任玉林. All rights reserved.
//

import UIKit

class LLComposerTextView: UITextView {
    
    //字体
   override var font: UIFont?{
        
        didSet{
            
            placeholderLabel.font = font
        }
    }
    
    //站位文字
    var placeholder: String?{
        
        didSet{
            
            placeholderLabel.text = placeholder
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //设置视图
    func setupUI(){
       
        addSubview(placeholderLabel)
        
        placeholderLabel.backgroundColor = UIColor.whiteColor()
        
        //约束
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(8)
            
            make.leading.equalTo(self).offset(5)
            
            make.width.equalTo(SCREENW - 10)
        }
        
        //监听textView是否有写入文子
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "TextViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    
    //移除通知
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //监听方法
    func TextViewTextDidChange(){
        
        placeholderLabel.hidden = hasText()
    }
    
    //懒加载
    private lazy var placeholderLabel: UILabel = {
        
        let lab = UILabel()
        
        lab.textColor = UIColor.grayColor()
        
        lab.numberOfLines = 0
        
        lab.font =  UIFont.systemFontOfSize(12)
        
        return lab
    }()

}
